// pages/orderDetail/orderDetail.js
var app = getApp(), api = require("../../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    Timeout:false,
    Timeout1: false,
    load:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order_no = options.order_no,_this=this;
    var x='';
    function countSecond() {
       x = (x == '...') ? '.' : (x + '.');
      console.log(x);
      _this.setData({
        load: x
      })
      _this.setData({
        Timeout1:setTimeout(countSecond, 1500)
      })
     
    }
    countSecond()
    if(!order_no)return false;
    clearTimeout(this.data.Timeout);
    function time() {
      _this.details(order_no);
      _this.setData({
        Timeout: setTimeout(time, 20000)
      })
    }
    time()
  },
  onHide:function(){
    clearTimeout(this.data.Timeout1);
    clearTimeout(this.data.Timeout);
  },
  onUnload:function(){
    clearTimeout(this.data.Timeout1);
    clearTimeout(this.data.Timeout);
  },
  details:function(order_no){
    var _this=this;
    app.request({
      url: api.order_details,
      data: {
        order_no: order_no
      },
      method: 'post',
      success: function (res) {
        _this.setData({
          data: res.data
        })
        if (res.data.is_activation==1&&res.data.is_send!=0){
            clearTimeout(_this.data.Timeout)
        }
      }
    })
  },
  clickPay: function () {
    var that = this
    that.setData({
      isShow: true
    })
  },
  feedback:function(){
    var _this=this,order_no = _this.data.data.order_no;
    if(!order_no)return false;
      wx.navigateTo({
        url: '/pages/my/idea/idea?order_no='+order_no,
      })
  },
  handleClose: function () {
    var that = this
    that.setData({
      isShow: false
    })
  },
  is_send:function(){
    var _this=this;
    wx.showModal({
      title: '提示',
      content: '您确定结束订单吗?',
      success:function(res){
      
        if(res.confirm){
          app.request({
            url: api.send_order,
            data:{
              order_no:_this.data.data.order_no
            },
            method:'post',
            success:function(res){
              _this.details(_this.data.data.order_no);
              res.code==0&&wx.showToast({
                title: '结束成功',
                success:function(){
                  setTimeout(function(){
                    _this.setData({
                      isShow: true
                    })
                  },1000)
                }
              })
              
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  payment:function(e){
    var _this = this, type = e.currentTarget.dataset.type;
    if(!type)return false;
    wx.showModal({
      title: '提示',
      content: '您需要支付'+_this.data.data.price+'元',
      success:function(res){
        if(res.confirm){
          wx.showLoading({
            title: '请稍后',
          })
          app.request({
            url: api.payment,
            data: {
              pay_type: type,
              order_no: _this.data.data.order_no
            },
            method: 'post',
            success: function (res) {
              wx.hideLoading();
              if (res.code === 1) {
                wx.requestPayment({
                  timeStamp: res.data.timeStamp,
                  nonceStr: res.data.nonceStr,
                  package: res.data.package,
                  signType: res.data.signType,
                  paySign: res.data.paySign,
                  success: function (res) {
                    wx.showToast({
                      title: '支付成功',
                      icon: 'none'
                    })
                    _this.setData({
                      isShow:false
                    })
                    _this.details(_this.data.data.order_no);
                  }
                })
                return false;
              }
              if(res.code===0){
                wx.showToast({
                  title: '支付成功',
                  icon: 'none'
                })
                _this.setData({
                  isShow: false
                })
                _this.details(_this.data.data.order_no);
              }
              if (res.code === -2) {
                wx.showModal({
                  title: '余额不足',
                  content: '是否前往充值页面',
                  success:function(res){
                    if(res.confirm){
                      wx.navigateTo({
                        url: '/pages/my/topUp/topUp',
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
   
  },
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this=this;
    if(_this.data.data){
      _this.details(_this.data.data.order_no);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
 


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})